package com.polito.mad.bikeponyapp.persistence;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

public class QuestionDao extends AbstractDAO<Question, BikePonySQLiteHelper> {

	private String[]cols;
	public static final int ID_COLUMN_INDEX = 0;
	public static final int TEXT_COLUMN_INDEX = 2;
	public static final int QUESTION_ID_INDEX=1;
	
	public QuestionDao(Context ctx) {
		super(new BikePonySQLiteHelper(ctx, null,2));
		cols = new String[3];
		cols[ID_COLUMN_INDEX]="_id";
		cols[QUESTION_ID_INDEX]="questionId";
		cols[TEXT_COLUMN_INDEX]="_text";
	}

	@Override
	public List<Question> getAll() {
		
		List<Question> l=new ArrayList<Question>();
		Cursor c= getDb().query(BikePonySQLiteHelper.QUESTION_DB_TABLE_NAME,cols, null, null, null, null, null);
		c.moveToFirst();
		while (!c.isAfterLast()) {
			l.add(getCursor(c));
			c.moveToNext();
		}
		c.close();
		return l;
	}

	@Override
	public Question getCursor(Cursor c) {
		Question q = new Question();
		q.setId(c.getLong(ID_COLUMN_INDEX));
		q.setQuestionId(c.getLong(QUESTION_ID_INDEX));
		q.setText(c.getString(TEXT_COLUMN_INDEX));
		return q;
	}

	@Override
	public Question get(long id) {
		Cursor c = getDb().query(BikePonySQLiteHelper.QUESTION_DB_TABLE_NAME, cols, cols[ID_COLUMN_INDEX]+"=?", new String[]{String.valueOf(id)}, null, null, null);
		Question q = null;
		if(c.moveToFirst())
		{
			q =  getCursor(c);
		}
		c.close();
		return q;
	}

	@Override
	public Question add(Question entity) throws SQLException {
		ContentValues cValues = fillEntity(entity);
		Question item= null;
		getDb().beginTransaction();
		try
		{
			long id = getDb().insert(BikePonySQLiteHelper.QUESTION_DB_TABLE_NAME, null, cValues);
			if(id==-1)
				throw new SQLException("an error occurred during Question retrieval");
			item = get(id);
			getDb().setTransactionSuccessful();
		}finally
		{
			getDb().endTransaction();
		}
		return  item;
	}

	private ContentValues fillEntity(Question entity) {
		ContentValues cValues = new ContentValues();
		cValues.put(cols[TEXT_COLUMN_INDEX], entity.getText());
		cValues.put(cols[QUESTION_ID_INDEX], entity.getQuestionId());
		return cValues;
	}

	@Override
	public List<Question> addAll(List<Question> entities) throws SQLException {
		List<Question> newQuestions = new ArrayList<Question>();
		getDb().beginTransaction();
		try
		{
			for(Question question : entities)
			{
				newQuestions.add(add(question));
			}
			getDb().setTransactionSuccessful();
			return newQuestions;			
		}finally
		{
			getDb().endTransaction();
		}
	}

	@Override
	public void delete(Question entity) throws SQLException {
		getDb().beginTransaction();
		try
		{
			int deletedRows = getDb().delete(BikePonySQLiteHelper.QUESTION_DB_TABLE_NAME, cols[ID_COLUMN_INDEX]+"=?", new String[]{String.valueOf(entity.getId())});
			if(deletedRows==0)
				throw new SQLException("deliveryItem not deleted");
			getDb().setTransactionSuccessful();
		}finally
		{
			getDb().endTransaction();
		}
		
	}

	@Override
	public Question update(Question item) throws SQLException {
		getDb().beginTransaction();
		int affectedRows = getDb().update(BikePonySQLiteHelper.QUESTION_DB_TABLE_NAME, fillEntity(item), cols[ID_COLUMN_INDEX]+" = ?", new String[]{String.valueOf(item.getId())});
		try
		{
			if(affectedRows!=1)
			{
				throw new SQLException("unable to update the database");
			}
			item = get(item.getId());
			getDb().setTransactionSuccessful();
			return item;
		}finally
		{
			getDb().endTransaction();
		}
	}

	@Override
	public List<Question> updateAll(List<Question> items) throws SQLException {
		getDb().beginTransaction();
		try
		{
			for(Question item : items)
			{
				item = update(item);
			}
			getDb().setTransactionSuccessful();
			return items;
		}finally
		{
			getDb().endTransaction();
		}
	}

	@Override
	public int count() {
		Cursor c = getDb().query(BikePonySQLiteHelper.QUESTION_DB_TABLE_NAME, new String[]{"COUNT("+cols[ID_COLUMN_INDEX]+")"}, null, null, null, null, null);
		if(c.moveToFirst())
		{
			return c.getInt(0);
		}
		return -1;
	}
	public Cursor getAllAsCursor()
	{
		getDb().close();
		Cursor c= getDb().query(BikePonySQLiteHelper.QUESTION_DB_TABLE_NAME,cols, null, null, null, null, null);
		return c;
	}

	@Override
	public List<Question> findByQuery(String whereClause) {
		Cursor c = getDb().query(BikePonySQLiteHelper.QUESTION_DB_TABLE_NAME,null,whereClause,null,null,null,null);
		List<Question> questions = new ArrayList<Question>();
		if(c.moveToFirst())
		{
			while (!c.isAfterLast()) {
				questions.add(getCursor(c));
				c.moveToNext();
			}
		}
		c.close();
		return questions;
	}

	@Override
	protected String[] getColumnNames() {
		return cols;
	}

	@Override
	protected String getDbName() {
		return BikePonySQLiteHelper.BIKE_PONY_DB_NAME;
	}

	@Override
	public String getColumnNameByIndex(int colIndex) {
		return cols[colIndex];
	}

	@Override
	public int deleteAll(List<Question> questions) throws SQLException {
		int countDeleted =0;
		if(questions==null)
			return countDeleted;
		getDb().beginTransaction();
		try
		{
			for(Question item : questions)
			{
				delete(item);
				countDeleted++;
			}
			getDb().setTransactionSuccessful();
			return countDeleted;
		}finally
		{
			getDb().endTransaction();
		}
		
	}

}
